package com.future.myjavatools.core.zhoupu;

/**
 * Created by lizuju on 2017/10/8.
 */
public class ByteUtil {

    public final static int ZERO = 0;
    public final static int TWO = 2;
    public final static int FOUR = 4;
    public final static int EIGHT = 8;
    public final static int SIXTEEN = 16;
    public final static short BYTEMASK = 0xFF;

    public static String charsToHexString(char[] array) {
        if (ArrayUtil.isEmpty(array)) {
            return StringUtil.EMPTY;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (stringBuilder.length() > 0) {
                stringBuilder.append(",");
            }
            int v = array[i] & 0xFFFF;
            String hv = Integer.toHexString(v);
            stringBuilder.append("0x");
            if (hv.length() < FOUR) {
                for (int j = 0; j < FOUR - hv.length(); j++) {
                    stringBuilder.append(ZERO);
                }
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String bytesToHexString(byte[] array) {
        if (ArrayUtil.isEmpty(array)) {
            return StringUtil.EMPTY;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (stringBuilder.length() > 0) {
                stringBuilder.append(",");
            }
            int v = array[i] & BYTEMASK;
            String hv = Integer.toHexString(v);
            stringBuilder.append("0x");
            if (hv.length() < TWO) {
                stringBuilder.append(ZERO);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String charsToBinaryString(char[] array) {
        if (ArrayUtil.isEmpty(array)) {
            return StringUtil.EMPTY;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (stringBuilder.length() > 0) {
                stringBuilder.append(",");
            }
            int v = array[i] & 0xFFFF;
            String bv = Integer.toBinaryString(v);
            if (bv.length() < SIXTEEN) {
                for (int j = 0; j < SIXTEEN - bv.length(); j++) {
                    stringBuilder.append(ZERO);
                }
            }
            stringBuilder.append(bv);
        }
        return stringBuilder.toString();
    }

    public static String bytesToBinaryString(byte[] array) {
        if (ArrayUtil.isEmpty(array)) {
            return StringUtil.EMPTY;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (stringBuilder.length() > 0) {
                stringBuilder.append(",");
            }
            int v = array[i] & BYTEMASK;
            String bv = Integer.toBinaryString(v);
            if (bv.length() < EIGHT) {
                for (int j = 0; j < EIGHT - bv.length(); j++) {
                    stringBuilder.append(ZERO);
                }
            }
            stringBuilder.append(bv);
        }
        return stringBuilder.toString();
    }
}
